package com.tom.architect02.zframework.wms.skin.higher_lib.utils;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;


import java.lang.reflect.Method;
import java.util.ArrayList;

public class SkinManager {
    private static SkinManager skinManager = new SkinManager();

    public Resources mSkinResources;

    public Resources mAppResources;

    private Context mContext;

    public boolean isDefaultSkin;

    public String mSkinPkgName;

    public static SkinManager getInstance() {
        return skinManager;
    }

    public void init(Context context) {
        mContext = context;
        SkinPreference.init(context);
        String skin = SkinPreference.getInstance().getSkin();
        SkinManager.getInstance().loadSkinApk(skin);
    }

    public void loadSkinApk(String path) {
        try {
            isDefaultSkin = TextUtils.isEmpty(path);
            mAppResources = mContext.getResources();
            AssetManager assetManager = AssetManager.class.newInstance();
            Method addAssetPath = assetManager.getClass().getMethod("addAssetPath",
                    String.class);
            addAssetPath.invoke(assetManager, path);

            //mSkinResources 是我们的插件化目标APKresources
            mSkinResources = new Resources(assetManager, mAppResources.getDisplayMetrics
                    (), mAppResources.getConfiguration());
            PackageManager mPm = mContext.getPackageManager();
            PackageInfo info = mPm.getPackageArchiveInfo(path, PackageManager
                    .GET_ACTIVITIES);
            mSkinPkgName = info.packageName;
            SkinPreference.getInstance().setSkin(path);
        } catch (Exception e) {

        }
    }


    /**
     * @param resId
     * @return
     */
    public int getIdentifier(int resId) {
        if (isDefaultSkin) {
            return resId;
        }
        //bg
        String resName = mAppResources.getResourceEntryName(resId);
        //
        String resType = mAppResources.getResourceTypeName(resId);
        //再通过bg 去找目标apk的bg
        int skinId = mSkinResources.getIdentifier(resName, resType, mSkinPkgName);
        return skinId;
    }

    public int getColor(int resId) {
        if (isDefaultSkin) {
            return mAppResources.getColor(resId);
        }
        int skinId = getIdentifier(resId);
        if (skinId == 0) {
            return mAppResources.getColor(resId);
        }
        return mSkinResources.getColor(skinId);
    }

    public ColorStateList getColorStateList(int resId) {
        if (isDefaultSkin) {
            return mAppResources.getColorStateList(resId);
        }
        int skinId = getIdentifier(resId);
        if (skinId == 0) {
            return mAppResources.getColorStateList(resId);
        }
        return mSkinResources.getColorStateList(skinId);
    }

    public Drawable getDrawable(int resId) {
        if (isDefaultSkin) {
            return mAppResources.getDrawable(resId);
        }
        //通过 app的resource 获取id 对应的 资源名 与 资源类型
        //找到 皮肤包 匹配 的 资源名资源类型 的 皮肤包的 资源 ID
        int skinId = getIdentifier(resId);
        if (skinId == 0) {
            return mAppResources.getDrawable(resId);
        }
        return mSkinResources.getDrawable(skinId);
    }


    /**
     * 可能是Color 也可能是drawable
     *
     * @return
     */
    public Object getBackground(int resId) {
        String resourceTypeName = mAppResources.getResourceTypeName(resId);
        if ("color".equals(resourceTypeName)) {
            return getColor(resId);
        } else {
            // drawable
            return getDrawable(resId);
        }
    }

}
